<?php

namespace app\controllers\h5;

use app\controllers\h5\base\H5Controller;
use app\models\Activity;
use app\models\News as Model;
use app\util\Util;

class NewsController extends H5Controller
{
    /**
     * news list
     *
     * @return array|string[]|\yii\web\Response
     */
    public function actionList()
    {
        $page = intval($this->post('page', $this->page));
        $pageSize = intval($this->post('pageSize', $this->pageSize));

        $query = Model::find()
            ->where(['status' => Model::$statusMapping['OK']]);

        $totalCount = $query->count();
        $totalPage = ceil($totalCount / $pageSize);

        $list = $query->orderBy(['sort' => SORT_ASC, 'id' => SORT_DESC])
            ->offset(($page - 1) * $pageSize)
            ->limit($pageSize)
            ->with(['image'])
            ->asArray()
            ->all();

        return $this->successEncode([
            $this->list => $list,
            $this->totalCount => (int)$totalCount,
            $this->totalPage => $totalPage,
            $this->currentPage => $page
        ]);
    }

    /**
     * news view
     *
     * @return array|string[]|\yii\web\Response
     */
    public function actionView()
    {
        $id = $this->post('id');

        $model = Model::find()
            ->where(['id' => $id])
            ->andWhere(['status' => Model::$statusMapping['OK']])
            // ->with(['image'])
            ->asArray()
            ->one();
        if ($model) {
            // convert images
            $convert = Util::convertImages($model['content']);
            $model['inImages'] = $convert['images'];
            $model['inText'] = $convert['text'];

            unset($model['content']);

            return $this->successEncode($model);
        }

        return $this->nothing();
    }

    // 报名
    public function actionActivity()
    {
        $news_id = $this->post('news_id');
        $user_id = $this->user->id;

        $sex = $this->post('sex');
        $is_money = $this->post('money');
        $name = $this->post('name');



        $activity = Activity::findOne(['user_id' => $user_id, 'news_id' => $news_id]);
        if ($activity) {
            return $this->fail('您已经报过名了!');
        }

        $activity = new Activity();
        $activity->user_id = $user_id;
        $activity->news_id = $news_id;
        $activity->sex = $sex;
        $activity->name = $name;
        $activity->is_money = $is_money;

        if ($activity->save()) {
            return $this->success();
        }
    }

    // 签到报名
    public function actionRunActivity()
    {
        $news_id = $this->post('news_id');
        $user_id = $this->user->id;


        $activity = Activity::findOne(['user_id' => $user_id, 'news_id' => $news_id]);
        if (!$activity) {
            return $this->fail('您没有报过名!');
        }

        if ($activity->status == 1) {
            return $this->fail('您已经签到过了,请勿重复操作!');
        }

        $activity->status = 1;

        if ($activity->save()) {
            return $this->success();
        }
    }
}